*
* This Stata file reads in data written out by the Gauss files 
* MAIN.PRG and IMPUTEY.PRG. It creates a Stata dataset called
* UKOUT.DTA.
* Michael Tomz and Jason Wittenberg, 12/20/2001
*

* 
* Read in ukout2.out, generated in IMPUTEY.PRG
*
version 7.0
clear
set mem 10m
tempfile ukout2
infile distid year y1 y2 using ukout2.out
label var y1          "Transformed vote for Cons"
label var y2          "Transformed vote for Lab"
sort year distid
save `ukout2', replace


* 
* Read in ukout1.out, generated in MAIN.PRG
*
#delimit ;  
infile distid year y1lag y2lag
    coninc labinc libinc v1 v2 v3 
    double(ev1 pv1  ev2  pv2  ev3 pv3) tdf double(ey1 ey2)
    ts11 ts12 ts22 double(msecon mselab mselib)
    pcpfc1 pcpfc pcpfc2 pcppc1 pcppc pcppc2
    fully  using ukout1.out, clear;
#delimit cr

sort year distid
merge year distid using `ukout2'
tab _merge
drop _merge


gen str10 yearst = string(year)
replace yearst = "74a" if yearst=="74.9"                 /* year as string variable */
sort yearst
by yearst: gen partial = cond(v1>0 & v2>0 & v3==0,1,0)   /* marker for partially contested: Con vs. Lab */
drop if fully==0 & partial==0                            /* only 2 examples in all years, reduces obs from 4829 to 4827 */
gen ypc = ln(v1/v2) if partial==1                        /* y for partially contested districts */

/* Label variables */
label var distid      "District Identifier"
label var year        "Year of Election"
label var yearst      "Year as a String"
label var fully       "Fully contested=1, otherwise 0"
label var partial     "Partially contested: Con v. Lab"
label var v1          "Actual vote for Cons"
label var v2          "Actual vote for Lab"
label var v3          "Actual vote for Libs"
label var y1lag       "Lagged Y for Cons"
label var y2lag       "Lagged Y for Lab"
label var ypc         "Y where partially contested"
label var coninc      "Dummy for Cons incumbent"
label var labinc      "Dummy for Lab incumbent"
label var libinc      "Dummy for lib incumbent"
label var ey1         "E(y1)"
label var ey2         "E(y2)"                        
label var ev1         "Expected vote for Cons"
label var ev2         "Expected vote for Labor"
label var ev3         "Expected vote for Libs"
label var tdf         "Estimated DOF"
label var ts11        "Sigma 11"
label var ts12        "Sigma 12"
label var ts22        "Sigma 22"
label var pcpfc       "Avg % correct predicted (fully)"
label var pcpfc1      "Lower 95CI for pcpfc (fully"
label var pcpfc2      "Upper 95CI for pcpfc (fully)"
label var pcppc       "Avg % correct predicted (part)"
label var pcppc1      "Lower 95CI for pcppc (part)"
label var pcppc2      "Upper 95CI for pcppc (part)"

local order distid year yearst fully partial v1 v2 v3 y1 y2  /* 
   */ y1lag y2lag ypc coninc labinc libinc ey1 ey2 ev1 ev2 /*
   */ ev3 tdf ts11 ts12 ts22 pcpfc pcpfc1 pcpfc2 pcppc pcppc1 pcppc2
keep `order'
order `order'
save ukout, replace old
